﻿namespace KeRui.Common
{
    using System;
    using System.Data;
    using System.IO;
    using System.Text;
    using System.Xml;

    public abstract class XMLComponent
    {
        private string fileEncode = "utf-8";
        private string fileName = "MyFile.xml";
        private string fileOutputPath = "";
        private int indentation = 6;
        private string key = "ItemID";
        private string parentField = "Item";
        private DataTable sourceDT = null;
        private string startElement = "channel";
        private string version = "2.0";
        public XmlDocument xmlDoc_Metone = new XmlDocument();
        private string xslLink = null;

        protected XMLComponent()
        {
        }

        protected void AppendChildElement(string strName, string strInnerText, XmlElement parentElement)
        {
            this.AppendChildElement(strName, strInnerText, parentElement, this.xmlDoc_Metone);
        }

        protected void AppendChildElement(string strName, string strInnerText, XmlElement parentElement, XmlDocument xmlDocument)
        {
            XmlElement newChild = xmlDocument.CreateElement(strName);
            newChild.InnerText = strInnerText;
            parentElement.AppendChild(newChild);
        }

        protected void BulidXmlTree(XmlElement tempXmlElement, string location)
        {
            DataRow row = this.SourceDataTable.Select(this.Key + "=" + location)[0];
            XmlElement newChild = this.xmlDoc_Metone.CreateElement(this.ParentField);
            tempXmlElement.AppendChild(newChild);
            foreach (DataColumn column in this.SourceDataTable.Columns)
            {
                if (column.Caption.ToString().ToLower() != this.ParentField.ToLower())
                {
                    this.AppendChildElement(column.Caption.ToString().Trim().ToLower(), row[column.Caption.Trim()].ToString().Trim(), newChild);
                }
            }
            foreach (DataRow row2 in this.SourceDataTable.Select(this.ParentField + "=" + location))
            {
                if (this.SourceDataTable.Select("item=" + row2[this.Key].ToString()).Length >= 0)
                {
                    this.BulidXmlTree(newChild, row2[this.Key].ToString().Trim());
                }
            }
        }

        public void CreatePath()
        {
            string fileOutPath;
            if (this.FileOutPath != null)
            {
                fileOutPath = this.FileOutPath;
                if (!Directory.Exists(fileOutPath))
                {
                    Utils.CreateDir(fileOutPath);
                }
            }
            else
            {
                fileOutPath = @"C:\";
                string str2 = DateTime.Now.ToString("yyyy-M").Trim();
                if (!Directory.Exists(fileOutPath + str2))
                {
                    Utils.CreateDir(fileOutPath + @"\" + str2);
                }
            }
        }

        public abstract string WriteFile();
        public abstract StringBuilder WriteStringBuilder();

        public string FileEncode
        {
            get
            {
                return this.fileEncode;
            }
            set
            {
                this.fileEncode = value;
            }
        }

        public string FileName
        {
            get
            {
                return this.fileName;
            }
            set
            {
                this.fileName = value;
            }
        }

        public string FileOutPath
        {
            get
            {
                return this.fileOutputPath;
            }
            set
            {
                if (value.LastIndexOf(@"\") != (value.Length - 1))
                {
                    this.fileOutputPath = value + @"\";
                }
            }
        }

        public int Indentation
        {
            get
            {
                return this.indentation;
            }
            set
            {
                this.indentation = value;
            }
        }

        public string Key
        {
            get
            {
                return this.key;
            }
            set
            {
                this.key = value;
            }
        }

        public string ParentField
        {
            get
            {
                return this.parentField;
            }
            set
            {
                this.parentField = value;
            }
        }

        public DataTable SourceDataTable
        {
            get
            {
                return this.sourceDT;
            }
            set
            {
                this.sourceDT = value;
            }
        }

        public string StartElement
        {
            get
            {
                return this.startElement;
            }
            set
            {
                this.startElement = value;
            }
        }

        public string Version
        {
            get
            {
                return this.version;
            }
            set
            {
                this.version = value;
            }
        }

        public string XslLink
        {
            get
            {
                return this.xslLink;
            }
            set
            {
                this.xslLink = value;
            }
        }
    }
}

